package com.night.administration.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.night.administration.excepyion.AreaConflictException;
import com.night.administration.mapper.*;
import com.night.administration.service.CourseManagerService;
import com.night.common.entity.PageBean;
import com.night.common.entity.administration.Classes;
import com.night.common.entity.administration.Course;
import com.night.common.entity.administration.CourseManager;
import com.night.common.entity.administration.vo.CourseManagerVo;
import com.night.common.entity.administration.vo.CourseMangerQueryVo;
import com.night.common.entity.student.SelectCourseManagerVo;
import com.night.common.entity.student.Student;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: CourseManagerServiceImpl
 * @Description: //TODO
 * @author: LZJ
 * @date: 2022/10/19-17:55
 * @version: v1.0
 */
@Service("CourseManagerService")
@Transactional
public class CourseManagerServiceImpl implements CourseManagerService {

    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private CourseManagerMapper courseManagerMapper;
    @Resource
    private ClassCourseMapper classCourseMapper;
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private ClassMapper classMapper;

    @Override
    public void judge(CourseManagerVo courseManagerVo) throws AreaConflictException {
        List<CourseManager> courseManagers = courseManagerMapper.getArea(courseManagerVo.getCourseManager().getCourseWeek(), courseManagerVo.getCourseManager().getCourseStartTime());
        for (CourseManager manager : courseManagers) {
            if (courseManagerVo.getCourseManager().getCourseStartTime().equals(manager.getCourseStartTime()) && courseManagerVo.getCourseManager().getArea().getId() == manager.getArea().getId()) {
                throw new AreaConflictException("教室已被占用");
            }
        }
        rabbitTemplate.convertAndSend("administration_exchange_topic", "afterArrangement", courseManagerVo);
    }

    @Override
    public void insert(CourseManagerVo courseManagerVo) {
        Course course = courseMapper.getCourse(courseManagerVo.getCourseManager().getCourse().getId());
        courseManagerVo.getCourseManager().setCourseName(course.getCourseName());
        String name = teacherMapper.getTeacherNameByTeacherId(courseManagerVo.getCourseManager().getTeacher().getId());
        courseManagerVo.getCourseManager().setTeacherName(name);
        courseManagerVo.getCourseManager().setCourseEndTime(new Time(courseManagerVo.getCourseManager().getCourseStartTime().getTime() + 3600000));
        courseManagerMapper.insert(courseManagerVo.getCourseManager());
        classCourseMapper.insert(courseManagerVo.getClassId(), courseManagerVo.getCourseManager().getId());
    }

    @Override
    public List<Student> ListStudentsByCourseManagerId(int courseManagerId) {
        return courseManagerMapper.getStudentByCourseManagerId(courseManagerId);
    }

    @Override
    public List<CourseManager> listCourseManagerByTeacher(CourseMangerQueryVo courseMangerQueryVo) {
        return courseManagerMapper.getCourseManagerByTeacher(courseMangerQueryVo);
    }

    @Override
    public PageBean<List<CourseManager>> getByCondition(SelectCourseManagerVo selectCourseManagerVo) {
        int pageSize = 10;
        PageBean<List<CourseManager>> pageBean = new PageBean<>();
        Page p = PageHelper.startPage(selectCourseManagerVo.getPage(), pageSize);
        CourseManager courseManager = selectCourseManagerVo.getCourseManager();
        if (courseManager.getCourseName() == null) {
            courseManager.setCourseName("");
        }
        List<CourseManager> data = courseManagerMapper.getByCondition(courseManager);
        pageBean.setData(data);
        pageBean.setCurrPage(selectCourseManagerVo.getPage());
        pageBean.setPageSize(pageSize);
        pageBean.setTotalNum((int) p.getTotal());
        pageBean.setTotalPages(p.getPages());
        return pageBean;
    }

    @Override
    public List<Classes> getClassByTeacherId(int teacherId) {
        //取出课程管理表中重复的课程
        List<CourseManager> courseManager = courseManagerMapper.getCourseManagerByTeacherId(teacherId);
        List<CourseManager> courses = new ArrayList<>();
        for (CourseManager manager : courseManager) {
            if (courses.isEmpty()) {
                courses.add(manager);
            } else {
                boolean isSame = false;
                for (CourseManager course : courses) {
                    if (manager.getId().equals(course.getId())) {
                        isSame = true;
                        break;
                    }
                }
                if (!isSame) {
                    courses.add(manager);
                }
            }
        }
        List<Classes> classes = new ArrayList<>();
        for (CourseManager c : courses) {
            List<Classes> class1 = classMapper.getClassByCourse(c.getId());
            for (Classes classes1 : class1) {
                classes.add(classes1);
            }
        }
        return classes;
    }

    @Override
    public int[] getStudentByCourseId(int courseId) {
        return courseManagerMapper.getStudentByCourseId(courseId);
    }

    @Override
    public List<CourseManager> getCourseByWeek(String week, int classId) {
        return courseManagerMapper.getCourseByWeek(week, classId);
    }

    @Override
    public List<CourseManager> getCourseByTeacherId(int teacherId, String week) {
        return courseManagerMapper.getCourseByTeacherIdAndWeek(week, teacherId);
    }

    public List<Time> checkTime(String week, int teacherId, int classId) {
        //判断所加课程时间是否冲突
        List<CourseManager> courseByWeek = courseManagerMapper.getCourseByWeek(week, classId);
        List<Time> times = times();
        for (CourseManager courseManagerWeek : courseByWeek) {
            for (int i = times.size() - 1; i >= 0; i--) {//List中不能用for循环直接得出元素，必须要倒着删
                if (courseManagerWeek.getCourseStartTime().equals(times.get(i))) {
                    times.remove(times.get(i));
                }
            }
        }
        //判断所选老师在所选时间段是否已有课程
        List<CourseManager> course = courseManagerMapper.getCourseByTeacherIdAndWeek(week, teacherId);
        for (CourseManager courseManager1 : course) {
            for (int i = times.size() - 1; i >= 0; i--) {
                if (courseManager1.getCourseStartTime().equals(times.get(i))) {
                    times.remove(times.get(i));
                }
            }
        }
        return times;
    }


    public List<Time> times() {
        List<Time> times = new ArrayList<>();
        times.add(Time.valueOf("08:00:00"));
        times.add(Time.valueOf("09:10:00"));
        times.add(Time.valueOf("10:20:00"));
        times.add(Time.valueOf("11:30:00"));
        times.add(Time.valueOf("14:30:00"));
        times.add(Time.valueOf("15:40:00"));
        times.add(Time.valueOf("16:50:00"));
        times.add(Time.valueOf("17:00:00"));
        return times;
    }
}
